package com.wo.android.pockybill;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.wo.android.pockybill.adapter.BillAdapter;
import com.wo.android.pockybill.ui.BillItemDialog;
import com.wow.wa.bill.serivce.BillService;
import com.wow.wa.bill.serivce.BillServiceImpl;
import com.wow.wa.bill.serivce.CommonComent;
import com.wow.wa.bill.serivce.CommonComentImpl;
import com.wow.wa.bill.vo.Bill;
import com.wow.wa.ui.ItemDialog;

public class BillActivity extends BaseActivity implements OnClickListener,
		OnItemClickListener {

	private static final String TAG = BillActivity.class.getName();
	private static final int FORURWARD_ADD = 100;
	// ---------service
	private BillService billService = null;
	private CommonComent commonComent = null;

	// ---------view
	private TextView useMoneyText = null;
	private ImageButton addBtn = null;
	private Button remainBtn = null;
	private TextView state = null;

	private ListView listView = null;
	private int notPaidBill = 0;// 尚未繳費帳單數量
	private int overDayBill = 0;// 逾期帳單數量
	private int closeBill = 0;// 已接近帳單

	// ----------vo
	private List<Bill> result = new ArrayList<Bill>();
	private BillAdapter adapter = null;

	public void showTitle(final String useMoney) {
		this.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				useMoneyText.setText(useMoney);

			}
		});
	}

	// 程式異常
	public void errMsg(String msg) {
		this.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub

			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == FORURWARD_ADD) {
			InitTask task = new InitTask();
			task.execute(this);
		}
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
	}

	public class InitTask extends AsyncTask<BillActivity, Void, Long> {

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
		}

		@Override
		protected Long doInBackground(BillActivity... params) {
			Long total = (long) 0.0;
			try {
				result.clear();
				result.addAll(billService.getBillWithNoPaid(BillActivity.this));

				for (Bill bill : result) {
					total += Long.parseLong(bill.getDollar());

				}
				notPaidBill = result.size();

				overDayBill = billService.getBillOverDay(BillActivity.this)
						.size();

			} catch (Exception e) {
				Log.e(TAG, "error=" + e);
				errMsg(BillActivity.this.getString(R.string.unknowError));
			}
			// TODO Auto-generated method stub
			return total;
		}

		@Override
		protected void onPostExecute(Long result) {

			// listView.setAdapter(adapter);
			adapter.notifyDataSetChanged();

			List<String> moneys = new ArrayList<String>();
			String money = result.toString();
			int total = money.length();

			for (int i = money.length(); i > 2; i -= 3) {
				moneys.add(money.substring(i - 3, i));
			}

			if (total % 3 != 0) {
				moneys.add(money.substring(0, total % 3));
			}

			Log.v(TAG, moneys.toString());
			Collections.sort(moneys, Collections.reverseOrder());
			useMoneyText.setText(StringUtils.join(moneys, ",") + "$");

			String out = getString(R.string.notPaidBill) + notPaidBill + "\n";
			out += getString(R.string.overDayBill) + overDayBill + "\n";
			out += getString(R.string.closeBill) + closeBill;

			state.setText(out);
			listView.setOnItemClickListener(BillActivity.this);
			super.onPostExecute(result);
		}
	}

	@Override
	protected void onStart() {
		Log.v(TAG, "onStart");

		super.onStart();
	}

	@Override
	protected void onRestart() {
		Log.v(TAG, "onRestart");
		super.onRestart();
	}

	@Override
	protected void onResume() {
		Log.v(TAG, "onResume");
		super.onResume();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.v(TAG, " onCreate");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_bill);
		this.getActionBar().hide();

		this.billService = new BillServiceImpl(this);
		this.commonComent = new CommonComentImpl(this);

		this.useMoneyText = (TextView) this.findViewById(R.id.useMoneyText);
		this.addBtn = (ImageButton) this.findViewById(R.id.addBtn);
		this.remainBtn = (Button) this.findViewById(R.id.remainBtn);
		// this.adapter = new BillAdapter(this);
		this.listView = (ListView) this.findViewById(R.id.billListView);
		// this.state = (TextView) this.findViewById(R.id.state);

		// -----event
		this.addBtn.setOnClickListener(this);

		this.useMoneyText.setText("0$");
		this.listView.setAdapter(adapter);

		String out = getString(R.string.notPaidBill) + notPaidBill + "\n";
		out += getString(R.string.overDayBill) + overDayBill + "\n";
		out += getString(R.string.closeBill) + closeBill;

		this.state.setText(out);

		final InitTask task = new InitTask();
		task.execute(this);
		// this.loadPage();
	}

	@Override
	protected void onStop() {
		Log.v(TAG, " onStop");
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		Log.v(TAG, " onDestroy");
		super.onDestroy();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		// getMenuInflater().inflate(R.menu.bill, menu);
		return true;
	}

	@Override
	public Class<? extends BaseActivity> toWhenError() {
		// TODO Auto-generated method stub
		return (Class<? extends BaseActivity>) com.wo.android.pockybill.util.Args.Activity.homeActivity;
	}

	public List<Bill> getResult() {
		return result;
	}

	public void setResult(List<Bill> result) {
		this.result = result;
	}

	@Override
	public void onClick(View v) {

		// TODO Auto-generated method stub

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

		String[] item = this.getResources().getStringArray(R.array.stateArray);
		new ItemDialog(result.get(arg2).getName(), this, item, null).show();

		// TODO Auto-generated method stub

	}
}
